home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / 3DGPL 1.0 / CODE / CLIPPER / CLIPP-3D.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-08  |  7.7 KB  |  196 lines  |  [TEXT/MACA]

  1. /** 3DGPL *************************************************\
  2.  *  ()                                                    *
  3.  *  3-D volume and Z clipping.                            *
  4.  *                                                        *
  5.  *  Defines:                                              *
  6.  *   C_volume_clipping       Out-of view volume elements; *
  7.  *                                                        *
  8.  *   C_line_z_clipping       Clipping a line;             *
  9.  *   C_polygon_z_clipping    View plane cliping.          *
  10.  *                                                        *
  11.  *  (6/1995) By Sergei Savchenko. (savs@cs.mcgill.ca).    *
  12.  *  Copyright (c) 1995 Sergei Savchenko.                  *
  13.  *  THIS SOURCE CODE CAN'T BE USED FOR COMERCIAL PURPOSES *
  14.  *  WITHOUT AUTHORISATION                                 *
  15. \**********************************************************/
  16.  
  17. #include <limits.h>                         /* INT_MAX & INT_MIN */
  18.  
  19. #ifdef __MWERKS__
  20. #include "hardware.h"           /* HW_copy_int stuff */
  21. #include "clipper.h"             /* clipping constants */
  22. #else
  23. #include "../hardware/hardware.h"           /* HW_copy_int stuff */
  24. #include "../clipper/clipper.h"             /* clipping constants */
  25. #endif
  26.  
  27. int C_3D_clipping;                          /* type of performed clipping */
  28.  
  29. /**********************************************************\
  30.  *  Hole element volume clipping,                         *
  31.  *                                                        *
  32.  *  RETURNS:  0 when array of passed vertices is for sure *
  33.  *  --------    outside the view volume;                  *
  34.  *           -1 when further clipping is required;        *
  35.  *            1 when element has a fair chanse to be      *
  36.  *              inside the view volume, and should be     *
  37.  *              clipped by 2-D clipping when rendering    *
  38.  *              onto the screen.                          *
  39.  *                                                        *
  40.  *               | z                                      *
  41.  *          \    |    /        View volume is a piramid   *
  42.  *           \   |   /         with 90 degree angle.      *
  43.  *            \  |  /                                     *
  44.  *        -x>z \ | /  x>z                                 *
  45.  *              \|/                                       *
  46.  *       --------+--------- x                             *
  47.  *       z<C_Z_CLIPPING_MIN                               *
  48.  *                                                        *
  49.  *  ADDITIONAL FUNCTIONS: 1) extraction of tuples &       *
  50.  *  ---------------------    FORMAT change                *
  51.  *  source:       n,a1,...,aN      where N=dimension-3    *
  52.  *  destanation:  x,y,z,a1,...,aN  (x,y,z extracted from  *
  53.  *  int *vertex array starting from index n)              *
  54. \**********************************************************/
  55.  
  56. int C_volume_clipping(register int *from,register int *to,
  57.                       int *vertex,int dimension,int length
  58.                      )
  59. {
  60.  register int i;
  61.  int xmin,ymin,zmin,xmax,ymax,zmax;
  62.  
  63.  dimension-=3;                              /* but X,Y,Z */
  64.  
  65.  ymin=xmin=zmin=INT_MAX;
  66.  ymax=xmax=zmax=INT_MIN;                    /* initializing searching */
  67.  
  68.  for(i=0;i<length;i++)
  69.  {
  70.   HW_copy_int(&vertex[*from++],to,3);       /* copying actual tuples */
  71.  
  72.   if(*to>xmax) xmax=*to;                    /* determining polygon extend */
  73.   if(*to<xmin) xmin=*to;
  74.   to++;
  75.   if(*to>ymax) ymax=*to;
  76.   if(*to<ymin) ymin=*to;
  77.   to++;
  78.   if(*to>zmax) zmax=*to;
  79.   if(*to<zmin) zmin=*to;                    /* by searching max/min */
  80.   to++;
  81.  
  82.   HW_copy_int(from,to,dimension);           /* rest (for shading etc)*/
  83.   to+=dimension;
  84.   from+=dimension;
  85.  }
  86.  
  87.  if((zmax<xmin)||(zmax<ymin)||(zmax<-xmax)||
  88.    (zmax<-ymax)||(zmax<=C_Z_CLIPPING_MIN))
  89.   return(0);                                /* outside */
  90.  else
  91.   if(zmin<C_Z_CLIPPING_MIN) return(-1);     /* partly behind clipping plane */
  92.   else return(1);
  93. }
  94.  
  95. /**********************************************************\
  96.  *  Line clipping using binary search technique.          *
  97.  *                                                        *
  98.  *  RETURNS: 0 element is outside the view volume;        *
  99.  *  -------  1 element is clipped to the view volume.     *
  100.  *                                                        *
  101.  *  SETS: C_3D_clipping to 1 if first vertex was clipped; *
  102.  *  -----               0 otherwise.                      *
  103. \**********************************************************/
  104.  
  105. int C_line_z_clipping(int **vertex1,int **vertex2,int dimension)
  106. {
  107.  register int i;
  108.  register int whereto;
  109.  register int *l,*r,*m,*t;                  /* left right and midle and tmp */
  110.  static int c_store0[C_MAX_DIMENSIONS];     /* static stores for clipped vxes */
  111.  static int c_store1[C_MAX_DIMENSIONS];
  112.  static int c_store2[C_MAX_DIMENSIONS];
  113.  int **vmn,**vmx;                           /* so that *vmn[3] < *vmx[3] */
  114.  int swap;                                  /* were coordinates swaped? */
  115.  
  116.  C_3D_clipping=0;                           /* default no clipping yet */
  117.  
  118.  if((*vertex1)[2]<(*vertex2)[2])            /* only Z counts 0=X,1=Y,2=Z,... */
  119.  { swap=0; vmn=vertex1; vmx=vertex2; }      /* so that *vmn[2] < *vmx[2] */
  120.  else
  121.  { swap=1; vmn=vertex2; vmx=vertex1; }
  122.  
  123.  if((*vmx)[2]<C_Z_CLIPPING_MIN) return(0);
  124.  else
  125.  {
  126.   if((*vmn)[2]<=C_Z_CLIPPING_MIN)           /* clipping */
  127.   {
  128.    HW_copy_int(*vmn,l=c_store0,dimension);  /* copying old vertices */
  129.    HW_copy_int(*vmx,m=c_store1,dimension);
  130.    r=c_store2;
  131.  
  132.    whereto=0;
  133.    while(m[2]!=C_Z_CLIPPING_MIN)
  134.    {
  135.     if(whereto==1) { t=l; l=m; m=t; }
  136.     else           { t=r; r=m; m=t; }
  137.     for(i=0;i<dimension;i++) m[i]=(l[i]+r[i])>>1;
  138.     whereto=m[2]<C_Z_CLIPPING_MIN;
  139.    }
  140.    *vmn=m;                                  /* that is why m[] is static */
  141.    C_3D_clipping=swap^1;
  142.   }
  143.   return(1);                                /* partialy or not clipped */
  144.  }
  145. }
  146.  
  147. /***********************************************************\
  148.  *  Creating a z-clipped polygon.                          *
  149.  *                                                         *
  150.  *  RETURNS: number of elements in the clipped polygon.    *
  151.  *  -------  (0 when compleately behind view plane)        *
  152.  *                                                         *
  153.  *          |            1-2-3-4-5-6-1  -> 2-2'-5'-6-1-2   *
  154.  *          |5'                                            *
  155.  *       5*-*-----*6     If first dot in the line is being *
  156.  *       /  |      \     clipped both points are copyed,   *
  157.  *     4*   |       *1   If no clipping or second point is *
  158.  *       \  |      /     clipped then only second point is *
  159.  *       3*-*-----*2     copyed. If both points are        *
  160.  *          |2'          clipped well, neither is copyed.  *
  161. \***********************************************************/
  162.  
  163. int C_polygon_z_clipping(register int *from,register int *to,
  164.                          int dimension,int length
  165.                         )
  166. {
  167.  register int i;
  168.  int *v1,*v2,new_lng=0;
  169.  int *first_vrtx=to;                        /* begining of the source */
  170.  
  171.  for(i=0;i<length;i++)                      /* for all edges */
  172.  {
  173.   v1=from; from+=dimension; v2=from;        /* taking two vertices */
  174.  
  175.   if(C_line_z_clipping(&v1,&v2,dimension))  /* clipping */
  176.   {
  177.    if(C_3D_clipping)                        /* depends which one was clipped */
  178.    {
  179.     HW_copy_int(v1,to,dimension); to+=dimension;
  180.     HW_copy_int(v2,to,dimension); to+=dimension;
  181.     new_lng+=2;                             /* first point clipped */
  182.    }
  183.    else
  184.    {
  185.     HW_copy_int(v2,to,dimension); to+=dimension;
  186.     new_lng++;                              /* second point clipped */
  187.    }
  188.   }
  189.  }
  190.  HW_copy_int(first_vrtx,to,dimension);      /* looping the polygon vertices */
  191.  
  192.  return(new_lng);
  193. }
  194.  
  195. /**********************************************************/
  196.